<template>
  <div>
    <el-drawer :title="title" :visible.sync="dialog" direction="rtl" ref="drawer" size="45%" :destroy-on-close="true"
      @close="close" :wrapperClosable="false">
      <div>
        <el-form ref="addForm" :model="formData" :rules="rules" size="small" label-width="100px">
          <el-form-item label="门店名称" prop="advertisingName">
            <el-input v-model="formData.advertisingName" placeholder="请输入门店名称" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
          <el-form-item label="类型" prop="typeId">
            <el-select v-model="formData.typeId" placeholder="请选择类型" clearable :style="{width: '100%'}"
              @change="handleTypeChange">
              <template v-for="item in typeOptions">
                <el-option :key="item.typeId" :label="item.typeName" :value="item.typeId"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="区域" prop="region">
            <el-cascader ref="regionRef" v-model="formData.region" :options="regionOptions"
              :props="{ value: 'id',label:'name' }" filterable clearable :style="{width: '100%'}" placeholder="请选择区域"
              @change="handleRegionChange">
            </el-cascader>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="formData.address" placeholder="请输入地址" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
          <el-form-item label="联系人" prop="contact">
            <el-input v-model="formData.contact" placeholder="请输入联系人" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
          <el-form-item label="电话" prop="iphone">
            <el-input v-model="formData.iphone" placeholder="请输入电话" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
          <el-form-item label="地图定位" prop="locationAddress">
            <el-input v-model="formData.locationAddress" placeholder="请选择定位" disabled :style="{width: '100%'}">
              <el-button slot="append" plain type="primary" @click="openMapIndex">打开地图</el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" :autosize="{minRows: 4, maxRows: 4}"
              :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-form>
        <div class="el-drawer-footer">
          <el-button size="small" @click="dialog = false">取 消</el-button>
          <el-button type="primary" size="small" @click="submitForm" :loading="loading">
            {{ loading ? '提交中 ...' : '提 交' }}
          </el-button>
        </div>
      </div>
    </el-drawer>

    <map-index ref="mapIndexRef"></map-index>
  </div>
</template>

<script>
  import MapIndex from '@/views/map/mapIndex.vue'
  import {
    addShop,
    updateShop,
    advertisingInfo,
  } from '@/api/advertising.js'
  import {
    getRegionTreeData
  } from "@/static/region.js"
  import {
    getShopTypeData
  } from "@/api/advertisingType.js"

  export default {
    components: {
      MapIndex
    },
    data() {
      return {
        dialog: false,
        loading: false,

        regionOptions: getRegionTreeData(),
        typeOptions: [],
        location: undefined,

        formData: {
          advertisingId: undefined,
          advertisingName: undefined,
          typePid: undefined,
          typeId: undefined,
          typeName: undefined,
          province: undefined,
          provinceName: undefined,
          city: undefined,
          cityName: undefined,
          area: undefined,
          areaName: undefined,
          address: undefined,
          contact: undefined,
          iphone: undefined,
          remark: undefined,
          locationId: undefined,
          locationAddress: undefined,
          region: [], //区域数组
        },
        rules: {
          advertisingName: [{
            required: true,
            trigger: 'blur',
            message: "请输入名称"
          }],
          typeId: [{
            required: true,
            trigger: 'change',
            message: "请选择类型"
          }],
          region: [{
            required: true,
            trigger: 'change',
            message: "请选择区域"
          }],
          address: [{
            required: true,
            trigger: 'blur',
            message: "请输入地址"
          }],
          contact: [{
            required: true,
            trigger: 'blur',
            message: "请输入联系人"
          }],
          iphone: [{
            required: true,
            trigger: 'blur',
            message: "请输入电话"
          }],
          locationAddress: [{
            required: true,
            trigger: 'change',
            message: "请选择定位"
          }]
        },
      }
    },
    computed: {
      title() {
        return this.formData.advertisingId ? '修改门店' : '添加门店';
      }
    },
    methods: {
      close() {
        this.loading = false;
        this.dialog = false;
        this.resetForm();
      },
      open(id) {
        this.resetForm();
        this.loading = false;
        this.dialog = true;
        this.formData.advertisingId = id;
        this.initData();
      },
      resetForm() {
        if (this.$refs.addForm !== undefined) {
          this.$refs.addForm.resetFields();
        }
        for (let key in this.formData) {
          this.formData[key] = undefined;
        }
      },
      setFormData(info) {
        for (let key in this.formData) {
          if (info.hasOwnProperty(key)) {
            this.formData[key] = info[key];
          }
        }
        this.formData.region = [info.province, info.city, info.area].map(i => i - 0);
        if (info.hasOwnProperty('location')) {
          this.location = info.location;
          this.formData.locationAddress = info.location.locationAddress;
        }
      },
      // 初始化数据
      async initData() {
        //类型
        var typeRes = await getShopTypeData();
        this.typeOptions = typeRes.data.children;
        //详情
        if (this.formData.advertisingId) {
          var infoRes = await advertisingInfo(this.formData.advertisingId);
          this.setFormData(infoRes.data);
        }
      },
      // 提交
      submitForm() {
        this.$refs.addForm.validate(async valid => {
          if (!valid) return false;
          this.loading = true;
          try {
            var {
              region,
              locationAddress,
              ...data
            } = this.formData;
            var params = Object.assign({}, data, this.location)
            var res;
            if (params.advertisingId) {
              //修改
              res = await updateShop(params);
            } else {
              //添加
              res = await addShop(params);
            }
            this.$utils.showSuccessMessage(res.msg, () => {
              this.close();
              this.$parent.searchTable();
            });
          } finally {
            this.loading = false;
          }
        })
      },
      handleTypeChange(value) {
        var option = this.typeOptions.filter(x => x.typeId == value)[0];
        this.formData.typeName = option.typeName;
        this.formData.typePid = option.typePid;
      },
      handleRegionChange(value) {
        var option3 = this.$refs.regionRef.getCheckedNodes(false)[0];
        var option2 = option3.parent;
        var option1 = option2.parent;
        this.formData.province = option1.data.id;
        this.formData.provinceName = option1.data.name;
        this.formData.city = option2.data.id;
        this.formData.cityName = option2.data.name;
        this.formData.area = option3.data.id;
        this.formData.areaName = option3.data.name;
      },

      openMapIndex() {
        this.$refs.mapIndexRef.open(this.location);
      },
      saveMapIndex(location) {
        this.location = location;
        this.formData.locationAddress = location.locationAddress;
      },

    },
  }
</script>

<style>
  .el-drawer__header {
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    color: #72767b;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    margin-bottom: 15px;
    padding: 15px 20px 0;
  }

  .el-drawer__body {
    height: calc(100% - 55px) !important;
    overflow-y: auto;
    padding: 5px;
    border-top: #DDDDDD 1px solid;
    margin-bottom: 62px;
    border-bottom: #DDDDDD 1px solid;
  }

  .el-drawer-footer {
    width: 100%;
    text-align: right;
    padding: 15px 20px;
    background-color: white;
    position: absolute;
    bottom: 0;
  }
</style>
